package cn.t.repository.impl.device;

import cn.t.converter.device.DeviceOpenRecordConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.enums.OptionTypeEnum;
import cn.t.model.device.DeviceOpenRecordDO;
import cn.t.persistence.entity.device.DeviceOpenRecordPO;
import cn.t.persistence.mapper.device.DeviceOpenRecordMapper;
import cn.t.repository.device.DeviceOpenRecordRepository;
import cn.t.utils.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 设备开门记录表(DeviceOpenRecord)数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class DeviceOpenRecordRepositoryImpl extends RepositoryImpl<DeviceOpenRecordMapper, DeviceOpenRecordPO, DeviceOpenRecordConverter, DeviceOpenRecordDO> implements DeviceOpenRecordRepository {


    @Override
    public List<DeviceOpenRecordDO> findByModel(DeviceOpenRecordDO deviceOpenRecordDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<DeviceOpenRecordPO> list = getWrapper(deviceOpenRecordDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<DeviceOpenRecordPO> getWrapper(DeviceOpenRecordDO deviceOpenRecordDO) {
        LambdaQueryChainWrapper<DeviceOpenRecordPO> wrapper = lambdaQuery();
        wrapper.eq(!ObjectUtils.isEmpty(deviceOpenRecordDO.getDeviceId()), DeviceOpenRecordPO::getDeviceId, deviceOpenRecordDO.getDeviceId());
        wrapper.in(!CollectionUtils.isEmpty(deviceOpenRecordDO.getDeviceIds()), DeviceOpenRecordPO::getDeviceId, deviceOpenRecordDO.getDeviceIds());
        wrapper.eq(!ObjectUtils.isEmpty(deviceOpenRecordDO.getBusinessType()), DeviceOpenRecordPO::getBusinessType, deviceOpenRecordDO.getBusinessType());
        wrapper.like(!ObjectUtils.isEmpty(deviceOpenRecordDO.getRecipient()), DeviceOpenRecordPO::getRecipient, deviceOpenRecordDO.getRecipient());
        wrapper.gt(!ObjectUtils.isEmpty(deviceOpenRecordDO.getStartAt()), DeviceOpenRecordPO::getGmtOpen, deviceOpenRecordDO.getStartAt());
        wrapper.lt(!ObjectUtils.isEmpty(deviceOpenRecordDO.getEndAt()), DeviceOpenRecordPO::getGmtOpen, deviceOpenRecordDO.getEndAt() + " 23:59:59");
        wrapper.eq(!ObjectUtils.isEmpty(deviceOpenRecordDO.getCardCode()), DeviceOpenRecordPO::getCardCode, deviceOpenRecordDO.getCardCode());
        return wrapper;
    }

    @Override
    public long count(DeviceOpenRecordDO deviceOpenRecordDO) {
        return getWrapper(deviceOpenRecordDO).count();
    }

    @Override
    public List<DeviceOpenRecordDO> getDeviceOpenRecordByOrderSn(List<String> orderSn) {
        LambdaQueryChainWrapper<DeviceOpenRecordPO> wrapper = lambdaQuery();
        wrapper.in( DeviceOpenRecordPO::getOrderSn, orderSn);
        List<DeviceOpenRecordPO> list = wrapper.list();
        return  converter.po2DOList(list);
    }

    @Override
    public List<DeviceOpenRecordDO> findWarning() {
        LambdaQueryChainWrapper<DeviceOpenRecordPO> wrapper = lambdaQuery();
        // 开门类型-领料开门
        wrapper.eq(DeviceOpenRecordPO::getBusinessType, OptionTypeEnum.order_open.getCode());
        // 未发送邮件
        wrapper.eq(DeviceOpenRecordPO::getIsSendWarningEmail, YesNoEnum.FALSE.getCode());
        // 开门时间小于(当前时间往前两小时) 并且 没关门
        wrapper.lt(DeviceOpenRecordPO::getGmtOpen, DateUtil.afterTime(LocalDateTime.now(), -2, TimeUnit.HOURS));
        wrapper.isNull(DeviceOpenRecordPO::getGmtClose);
        List<DeviceOpenRecordPO> list = wrapper.list();
        return  converter.po2DOList(list);
    }
    @Override
    public void updateToSendEmailMark(List<Long> ids) {
        LambdaUpdateWrapper<DeviceOpenRecordPO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(DeviceOpenRecordPO::getId,ids);
        wrapper.set(DeviceOpenRecordPO::getIsSendWarningEmail,YesNoEnum.TRUE);
        getBaseMapper().update(null,wrapper);
    }
}
